package com.wd.util;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StringUtil {

    private static Log log = LogFactory.getLog(StringUtil.class);

    public static boolean isValidString(String str) {
        if (str == null || str.trim().length() <= 0) {
            return false;
        }
        return true;
    }

    public static String replaceAllBlank(String str) {
        return str.replaceAll(" ", "");
    }

    public static String getStringByCharset(byte[] buf, String charset) {
        String str = null;
        try {
             str = new String(buf, charset);
        } catch (UnsupportedEncodingException e) {
            log.error("", e);
        }
        return str;
    }
    
    
    public static String filterSpecicalChar(String str) {
        if(!isValidString(str)) {
            return str;
        }
        str = str.replaceAll("&nbsp;", " ");
        str = str.replaceAll("&gt;", " ");
        str = str.replaceAll("&reg;", " ");
        Pattern p = Pattern.compile("\\s+");
        Matcher m = p.matcher(str);
        str = m.replaceAll("");
        return str;
    }
    
    public static String filterLn(String str) {
        if(!isValidString(str)) {
            return str;
        }
        Pattern p = Pattern.compile("[\r\n]+");
        Matcher m = p.matcher(str);
        str = m.replaceAll("");
        return str;
    }
    
    public static String filterSqlString(String str) {
        if(!isValidString(str)) {
            return str;
        }
        
        if(str.indexOf("'") >= 0) {
            str = str.replaceAll("'", "''");
        }
        return str;
    }
    
    
    
    public static void main(String args[]) {
//        String a = " aa &nbsp;' \n\taa";
//        System.out.println(a);
//        System.out.println(filterSpecicalChar(a));
    }

}
